package com.picturestory.client.android;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.Reader;
import java.io.StringWriter;
import java.io.Writer;

import org.apache.http.HttpResponse;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.DefaultHttpClient;
import org.json.JSONObject;

import com.picturestory.client.android.R;
import com.picturestory.client.android.util.Constants;

import android.app.Activity;
import android.app.ProgressDialog;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.Window;
import android.widget.Button;
import android.widget.TextView;

public class HttpConnectionTest extends Activity {
        private static final String TAG = HttpConnectionTest.class.getSimpleName();
        
        private TextView mTitle;
        private TextView mBody;
        private TextView mResult;
        private Button   mOK;
        private Button   mCancel;
        private ProgressDialog mProgressDialog;
        
        
        private Handler mPostHandler = new Handler() {

                private String post(String url) {
                        String result = null;
                        HttpClient client = new DefaultHttpClient();
                        HttpResponse response = null;
                        
                        HttpPost httpPost = new HttpPost(url);
                        httpPost.setHeader("Content-type", "text/plain");
                        httpPost.setHeader("Charset", "UTF-8");
                        
                        try {
                                response = client.execute(httpPost);
                        } catch (ClientProtocolException e) {
                                e.printStackTrace();
                        } catch (IOException e) {
                                e.printStackTrace();
                        }
                        
                        return response.getStatusLine().toString();
                }
                
                
                @Override
                public void handleMessage(Message msg) {
                        // TODO Auto-generated method stub
                        String result = post("http://picture-history.appspot.com/employee/mr/eric/kim");
                        mResult.setText(result);
                }
                
        };
        
        
        
        
        
        
        @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        
        Log.i(Constants.LOGTAG, " " + TAG + " is on create");
        
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        setContentView(R.layout.http_connection_test);
        
        mTitle  = (TextView) findViewById(R.id.title_entry);
                mBody   = (TextView) findViewById(R.id.body_entry);
                mOK     = (Button)   findViewById(R.id.ok);
                mCancel = (Button)   findViewById(R.id.cancel);
                
                mResult = (TextView) findViewById(R.id.result);
                
                mOK.setOnClickListener(new OnClickListener() {

                        @Override
                        public void onClick(View v) {
                        	startActivityForResult(
                        			new Intent(HttpConnectionTest.this, HttpRequester.class), 1);
                                
                        }
                        
                });
                
                
    }





		@Override
		protected void onActivityResult(int requestCode, int resultCode,
				Intent data) {
			mResult.setText(data.getStringExtra("result"));
		}
        
        
        
        
}
